package com.mv.encode;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.apache.commons.io.FileUtils;



public class FileEncodeConvert extends FileUtils {
 

	private enum ConvetType {
	    GBK2UTF8, UTF82GBK
	}
	private enum FileType {
		JAVA(".java"), HTML(".html"), OTHER("銆�*");

		 
	    private String value; 
		FileType(String string) {
			// TODO Auto-generated constructor stub
			this.value = string;
		} 
		
		public String getValue() { 
	        return this.value; 
	    }
		public FileType setValue(String string) { 
			this.value = string;
			return this;
	    }		
		
	}
 
	public static void convertGBK2UTF8(File file) {
		convertFileEncode(file, FileEncode.ENCODE_GBK, FileEncode.ENCODE_UTF8);
	}
 
	public static void convertUTF82GBK(File file) {
		convertFileEncode(file, FileEncode.ENCODE_UTF8, FileEncode.ENCODE_GBK);
	}
 
	public static void convertFileEncode(File file, String fromEncode,String toEncode) {
		try {
//			String fileEncode = FileEncode.getFileEncode(file.getAbsolutePath());
//			if(fromEncode.equals(fileEncode)) {
//				return;
//			}
			System.out.println("Convert File:" + file.getName());
			String str = readFileToString(file, fromEncode);
			writeStringToFile(file, str, toEncode);
		} catch (IOException e) {
			System.out.println("Convert failed. File not exsit?");
			e.printStackTrace();
		}
	}
 
	public static String readFile(String filePath, String encode) {
		String fileContent = "";
		try {
			FileInputStream fis = new FileInputStream(filePath);
			InputStreamReader isr = new InputStreamReader(fis, encode);
			BufferedReader br = new BufferedReader(isr);
			StringBuffer sb = new StringBuffer();
			for (String line = br.readLine(); line != null; line = br
					.readLine()) {
				sb.append(line);
			}
			fileContent = sb.toString();
			br.close();
			isr.close();
			fis.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not exist" + filePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileContent;
	}
 
	public static String saveFile(String fileContent, String encode,
			String savePath) {
		try {
			FileOutputStream fos = new FileOutputStream(savePath);
			OutputStreamWriter osw = new OutputStreamWriter(fos, encode);
			BufferedWriter bw = new BufferedWriter(osw);
 
			bw.write(fileContent);
			bw.close();
			osw.close();
			fos.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not exist" + savePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileContent;
	}
 

	public static synchronized void convertDirectory(File dir, ConvetType cvtType, FileType fileType)
			throws IOException {
		System.out.println("debug");
		if (!dir.exists() && !dir.isDirectory()) {
			throw new IOException("[" + dir + "] not exsit or not a Directory");
		}
		convert(dir, cvtType, fileType);
	}	
 
	public static void convert(File dir,ConvetType cvtType,FileType fileType) {
		if (!dir.canRead() ||!dir.canWrite()) {
			System.out.println("exit, dir cannot read or write");
			return;
		}
			if (dir.isDirectory()) {// Directory
				String[] files = dir.list();
				if (files == null) {
					System.out.println("files is empty, dir is " + dir.getAbsolutePath());
				}
				for (int i = 0; i < files.length; i++) {
					convert(new File(dir, files[i]),cvtType,fileType);// 閫掑綊
				}
			} else {// File
				if(fileType == null) {
					System.out.println("all file  convert current file:" + dir.getName());
					if(cvtType == ConvetType.GBK2UTF8) {
						convertGBK2UTF8(dir); 
					}else if(cvtType == ConvetType.UTF82GBK) {
						convertUTF82GBK(dir);
					}
				}else{
					if(!dir.getName().endsWith(fileType.getValue())){
						System.out.println(fileType.getValue() + "file convert skip file:" + dir.getName());
						
					}else{
						System.out.println(fileType.getValue() + "file convert current file:" + dir.getName());
						if(cvtType == ConvetType.GBK2UTF8) {
							convertGBK2UTF8(dir); 
						}else if(cvtType == ConvetType.UTF82GBK) {
							convertUTF82GBK(dir);
						}
					}
				}
			}
	}
 
	public static void main(String[] args)  {
 
		long t1 = System.currentTimeMillis();
		File src = new File("E:\\code\\net-git\\pooze\\pz-j2ee-framwork\\wh-spring");
		
		try {
			convertDirectory(src, ConvetType.GBK2UTF8, FileType.JAVA);
			//convertDirectory(src, ConvetType.GBK2UTF8, FileType.OTHER.setValue("ClientHandler.java"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long t2 = System.currentTimeMillis();
		System.out.println("Convert complete" + (t2 - t1) / 1000);
	}
 
}